/**
 * Program   :   BeanUtilsEx.java
 * Author    :   shadow guo
 * Create    :   2013-6-7 下午3:26:47
 *
 * Copyright 2008 by Tongfu Information Technology Co. Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Tongfu Information Technology Co. Ltd. ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Tongfu Information Technology Co. Ltd.
 * 
 */
package com.tongfusoft.utils;

import java.beans.PropertyDescriptor;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import net.sf.cglib.core.ReflectUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.tongfusoft.exception.ObjectConvertException;

public class BeanUtilsEx {
	/**
	 * Logger for this class
	 */
	private static final Log logger = LogFactory.getLog(BeanUtilsEx.class);

	public static void map2Bean(Map<String,Object> map ,Object dest){
		PropertyDescriptor[]  props = ReflectUtils.getBeanProperties(dest.getClass());
		Map<String,PropertyDescriptor> propertys = new HashMap<String,PropertyDescriptor>();
		for(PropertyDescriptor prop : props){
			propertys.put(prop.getName(), prop);
		}
		
		
		Iterator<Entry<String,Object>> entries = map.entrySet().iterator();
         while (entries.hasNext()) {
             Entry<String,Object> entry =  entries.next();
             String name = entry.getKey();
             if(propertys.containsKey(name) && entry.getValue() != null){
            	 //赋值
            	 ReflectionUtils.setFieldValue(dest, name, convertValue(propertys.get(name),entry.getValue()));
             }
             
         }
         
	}
	
	private static Object convertValue(PropertyDescriptor prop,Object value){
		if(prop.getPropertyType().equals(String.class)){
			return value.toString();
		}
		
		if(prop.getPropertyType().equals(Date.class)){
			if(value instanceof Date || value instanceof java.sql.Date){
				return value;
			}
			
			if(value instanceof String){
				try {
					return DateUtils.parse(value.toString(), "yyyy/MM/dd HH:mm:ss");
				} catch (ParseException e) {
					logger.error("parser date error:" + value);
				}
				
				try {
					return DateUtils.parse(value.toString(), "yyyy-MM-dd HH:mm:ss");
				} catch (ParseException e) {
					logger.error("parser date error:" + value);
				}
				
				try {
					return DateUtils.parse(value.toString(), "yyyy/MM/dd");
				} catch (ParseException e) {
					logger.error("parser date error:" + value);
				}
				
				try {
					return DateUtils.parse(value.toString(), "yyyy-MM-dd");
				} catch (ParseException e) {
					logger.error("parser date error:" + value);
				}
				
				throw new ObjectConvertException("date fromat error," + value);
			}
		}
		
		if(prop.getPropertyType().equals(Number.class) || prop.getPropertyType().equals(int.class) || prop.getPropertyType().equals(double.class)){
			try{
				return Integer.parseInt(value.toString());
			}catch(Exception e){
			}
			
			try{
				return Double.parseDouble(value.toString());
			}catch(Exception e){
			}
			
			throw new ObjectConvertException("number fromat error," + value);
		}
		
		return value;
	}
	
	
}
